﻿using EMoney.IStock.Client.Models.CQ;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EMoney.IStock.Client.DataAccess.CQ
{
    /// <summary>
    /// 连续竞猜结果数据访问类
    /// </summary>
    public class ContinueGuessDal
    {
        /// <summary>
        /// 新增连续竞猜结果
        /// </summary>
        /// <param name="continueGuess"></param>
        /// <returns></returns>
        public int AddContinueGuess(ContinueGuess continueGuess)
        {
            string sql = "insert into CQ_ContinueGuess(UserID,TradeDate,ContinueDays,GuessResult,UpdateTime) values(@UserID,@TradeDate,@ContinueDays,@GuessResult,getdate())";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("UserID", System.Data.SqlDbType.Int, continueGuess.UserID);
            tools.AddSqlParameter("TradeDate", System.Data.SqlDbType.DateTime, continueGuess.TradeDate);
            tools.AddSqlParameter("ContinueDays", System.Data.SqlDbType.Int, continueGuess.ContinueDays);
            tools.AddSqlParameter("GuessResult", System.Data.SqlDbType.Int, continueGuess.GuessResult);

            return SQLHelper.ExecuteNonQuery(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 更新连续竞猜结果
        /// </summary>
        /// <param name="continueGuess"></param>
        /// <returns></returns>
        public int UpdateContinueGuess(ContinueGuess continueGuess,DateTime preTradeDate)
        {
            string sql = "update CQ_ContinueGuess set TradeDate=@TradeDate,ContinueDays=@ContinueDays,UpdateTime=getdate() where UserID=@UserID and TradeDate=@PreTradeDate";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("UserID", System.Data.SqlDbType.Int, continueGuess.UserID);
            tools.AddSqlParameter("TradeDate", System.Data.SqlDbType.DateTime, continueGuess.TradeDate);
            tools.AddSqlParameter("ContinueDays", System.Data.SqlDbType.Int, continueGuess.ContinueDays);
            tools.AddSqlParameter("PreTradeDate", System.Data.SqlDbType.DateTime, preTradeDate);

            return SQLHelper.ExecuteNonQuery(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 获取用户连续猜对或猜错的最大天数
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public int GetMaxContinueDays(int userid, int result)
        {
            string sql = "select ISNULL(max(ContinueDays),0) from CQ_ContinueGuess where UserID=@UserID and GuessResult=@GuessResult";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("UserID", System.Data.SqlDbType.Int, userid);
            tools.AddSqlParameter("GuessResult", System.Data.SqlDbType.Int, result);

            return Convert.ToInt32(SQLHelper.ExecuteScalar(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql, tools.GetSqlParameters()));
        }

        /// <summary>
        /// 获取连续竞猜结果
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="tradeDate"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public ContinueGuess GetContinueGuess(int userid, DateTime tradeDate, int result)
        {
            string sql = "select * from CQ_ContinueGuess where UserID=@UserID and TradeDate=@TradeDate and GuessResult=@GuessResult";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("UserID", System.Data.SqlDbType.Int, userid);
            tools.AddSqlParameter("TradeDate", System.Data.SqlDbType.DateTime, tradeDate);
            tools.AddSqlParameter("GuessResult", System.Data.SqlDbType.Int, result);

            return SQLHelper.GetDataByColumns<ContinueGuess>(ConnectionManager.Emfstock, sql, tools.GetSqlParameters());
        }
    }
}
